home *** CD-ROM | disk | FTP | other *** search
- unit Hints2U;
- {$ifdef Ver80} { Delphi 1.0x }
- {$define DelphiLessThan3}
- {$endif}
- {$ifdef Ver90} { Delphi 2.0x }
- {$define DelphiLessThan3}
- {$endif}
- {$ifdef Ver93} { C++ Builder 1.0x }
- {$define DelphiLessThan3}
- {$endif}
-
- interface
-
- uses
- WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Mask, Grids, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- SG: TStringGrid;
- Timer1: TTimer;
- procedure FormCreate(Sender: TObject);
- procedure SGMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- procedure Timer1Timer(Sender: TObject);
- private
- GridHintWnd: THintWindow;
- function CalcHintRect(MaxWidth: Integer;
- const AHint: string; H: THintWindow): TRect;
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- {$ifdef DelphiLessThan3}
- { The private routine Forms.ForegroundTask was only made }
- { available in Delphi 3. This is a cheap'n'nasty version of it }
- function ForegroundTask: Boolean;
- begin
- Result := FindControl(GetActiveWindow) <> nil
- end;
- {$endif}
-
- function TForm1.CalcHintRect(MaxWidth: Integer;
- const AHint: string; H: THintWindow): TRect;
- {$ifdef DelphiLessThan3}
- var
- Buf: array[0..511] of Char;
- begin
- Result := Rect(0, 0, MaxWidth, 0);
- DrawText(H.Canvas.Handle, StrPCopy(Buf, AHint), -1, Result,
- DT_CALCRECT or DT_LEFT or DT_WORDBREAK or DT_NOPREFIX);
- Inc(Result.Right, 6);
- Inc(Result.Bottom, 2);
- {$else}
- begin
- Result := H.CalcHintRect(Screen.Width, AHint, nil)
- {$endif}
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var
- Loop, Loop2: Integer;
- begin
- for Loop := 1 to SG.RowCount-1 do
- for Loop2 := 1 to SG.ColCount-1 do
- SG.Cells[Loop2, Loop] := Format('(%d, %d)', [Loop2, Loop]);
- end;
-
- procedure TForm1.SGMouseMove(Sender: TObject; Shift: TShiftState; X,
- Y: Integer);
- const
- TextOffset = 2;
- var
- Col, Row: Longint;
- R, OldR: TRect;
- Pt: TPoint;
- begin
- { Check cell under mouse }
- SG.MouseToCell(X, Y, Col, Row);
- { If it is a cell, and text is bigger than }
- { screen space and in-place editor not present }
- if (Col <> -1) and (Row <> -1) and
- (SG.Canvas.TextWidth(SG.Cells[Col, Row]) + TextOffset > SG.ColWidths[Col]) and
- not SG.EditorMode and ForegroundTask then
- begin
- { Make sure hint window exists }
- if not Assigned(GridHintWnd) then
- begin
- GridHintWnd := HintWindowClass.Create(Self);
- GridHintWnd.Color := Application.HintColor;
- end;
- { Set hint text }
- SG.Hint := SG.Cells[Col, Row];
- { Calculate rect size }
- R := CalcHintRect(Screen.Width, SG.Hint, GridHintWnd);
- { Find target location }
- Pt := SG.ClientToScreen(SG.CellRect(Col, Row).TopLeft);
- { Tweak position so it is the same as the grid cell (hopefully) }
- {$ifdef DelphiLessThan3}
- Inc(Pt.Y);
- {$else}
- Dec(Pt.X);
- Dec(Pt.Y);
- {$endif}
- OffsetRect(R, Pt.X, Pt.Y);
- { Only draw it if it has moved - compare top-left }
- { (could compare whole rect but the hint sometimes grows itself) }
- GetWindowRect(GridHintWnd.Handle, OldR);
- if not IsWindowVisible(GridHintWnd.Handle) or
- not ((R.Left = OldR.Left) and (R.Top = OldR.Top)) then
- GridHintWnd.ActivateHint(R, SG.Hint)
- end
- else
- if Assigned(GridHintWnd) then
- GridHintWnd.ReleaseHandle
- end;
-
- procedure TForm1.Timer1Timer(Sender: TObject);
- var
- Pt: TPoint;
- begin
- GetCursorPos(Pt);
- Pt := ScreenToClient(Pt);
- if not PtInRect(SG.BoundsRect, Pt) and Assigned(GridHintWnd) then
- GridHintWnd.ReleaseHandle
- end;
-
- end.
-